package cn.com.tianpeidong.restaurantadmin.project.admin.dishes.rest;

import cn.com.tianpeidong.restaurantadmin.common.util.page.PageWrapper;
import cn.com.tianpeidong.restaurantadmin.project.admin.dishes.entity.Dishes;
import cn.com.tianpeidong.restaurantadmin.project.admin.dishes.service.IDishesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * @author tianpeidong
 * @version 创建时间: 19-6-1 上午11:55
 */
@RestController
@RequestMapping("/dishes")
public class DishesController {

    @Autowired
    private IDishesService dishesService;

    /**
     * 查看菜品/套餐
     * @return
     */
    @GetMapping
    @RequiresPermissions("dishes:list")
    public ResponseEntity listDishes(
            @RequestParam(value = "pagi") boolean isPagi,
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer page) {
        if (! isPagi) {
            return this.listAllDishes();
        }
        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<Dishes> dishesList = dishesService.listDishes(new Dishes());
        return ResponseEntity.ok(pageWrapper.getResult(dishesList));

    }

    @PostMapping("/search")
    @RequiresPermissions("dishes:list")
    public ResponseEntity search(
            @RequestBody Dishes dishes,
            @RequestParam(value = "per_page", required = false) Integer pageSize,
            @RequestParam(value = "page", required = false) Integer page) {
        PageWrapper pageWrapper = new PageWrapper(pageSize, page);
        List<Dishes> dishesList = dishesService.listDishes(dishes);
        return ResponseEntity.ok(pageWrapper.getResult(dishesList));
    }

    @GetMapping("/{id}")
    @RequiresPermissions("dishes:list")
    public ResponseEntity getDishesById(@PathVariable("id") Long id) {
        Dishes dishesVo = dishesService.getDishesById(id);
        return ResponseEntity.ok(dishesVo);
    }

    /**
     * 添加菜品
     * @param addDishes
     * @return
     */
    @PostMapping
    @RequiresPermissions("dishes:add")
    public ResponseEntity addDishes(@Valid @RequestBody Dishes addDishes) {
        Dishes newDishes = dishesService.addDishes(addDishes);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(newDishes.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    /**
     * 删除菜品
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @RequiresPermissions("dishes:delete")
    public ResponseEntity deleteDishes(@PathVariable("id") Long id) {
        dishesService.deleteDishes(id);
        return ResponseEntity.noContent().build();
    }

    /**
     * 编辑菜品
     * @param dishes
     * @return
     */
    @PutMapping
    @RequiresPermissions("dishes:edit")
    public ResponseEntity editDishes(@Valid @RequestBody Dishes dishes) {
        Dishes editDishes = dishesService.editDishes(dishes);
        URI location = ServletUriComponentsBuilder.fromCurrentRequest()
                .path("/{id}")
                .buildAndExpand(editDishes.getId())
                .toUri();
        return ResponseEntity.created(location).build();
    }

    /**
     * 列出系统已有菜品名
     * @return
     */
    private ResponseEntity listAllDishes() {
        List<Dishes> dishes = dishesService.listDishes(new Dishes());
        return ResponseEntity.ok(dishes);
    }
}
